package com.sendpo.zjx.sendpo.activity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.google.gson.Gson;
import com.sendpo.zjx.sendpo.BuildConfig;
import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.domain.UserDomain;
import com.sendpo.zjx.sendpo.net.HttpCode;
import com.sendpo.zjx.sendpo.net.HttpUrl;
import com.sendpo.zjx.sendpo.table.User;
import com.sendpo.zjx.sendpo.utils.Tools;
import com.sendpo.zjx.sendpo.utils.Xutils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.shaohui.bottomdialog.BottomDialog;

/**
 * Created by Administrator on 2017/8/31.
 */

public class UserShimingActivity extends AppCompatActivity {
    File temp_cmera = null;
    BottomDialog bottomDialog, bottomDialog1;

    public final int REQUEST_IMAGECOUNT = 1031;
    public final int REQUEST_CAMERA = 104;
    public final int REQUEST_CROP = 105;

    public final int REQUEST_IMAGECOUNT1 = 1032;
    public final int REQUEST_CAMERA1 = 107;
    public final int REQUEST_CROP1 = 106;

    public final int REQUEST_IMAGECOUNT2 = 1033;
    public final int REQUEST_CAMERA2 = 108;
    public final int REQUEST_CROP2 = 109;

    @BindView(R.id.head)
    RelativeLayout head;
    @BindView(R.id.real_name_tv)
    TextView realNameTv;
    @BindView(R.id.realname_edit)
    EditText realnameEdit;
    @BindView(R.id.number_name_tv)
    TextView numberNameTv;
    @BindView(R.id.card_edit)
    EditText cardEdit;
    @BindView(R.id.shenfen_title)
    TextView shenfenTitle;
    @BindView(R.id.zhengmian_iv)
    ImageView zhengmianIv;
    @BindView(R.id.zhengmian_bt)
    RelativeLayout zhengmianBt;
    @BindView(R.id.fanmian_iv)
    ImageView fanmianIv;
    @BindView(R.id.fanmian_bt)
    RelativeLayout fanmianBt;
    @BindView(R.id.shouchi_iv)
    ImageView shouchiIv;
    @BindView(R.id.shouchi_bt)
    RelativeLayout shouchiBt;
    private String temppath1 = "";
    private String temppath2 = "";
    private String temppath3="";
    public Dialog loadingdialog = null;
    private BottomDialog bottomDialog2=null;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Tools.initStatus(this, R.color.black);
        loadingdialog = new Dialog(this);
        loadingdialog.setContentView(R.layout.dialog_loading);
        loadingdialog.setCanceledOnTouchOutside(false);
        loadingdialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setContentView(R.layout.activity_usershiming);
        ButterKnife.bind(this);
        initComponent();
    }

    private void initComponent() {
        bottomDialog = BottomDialog.create(getSupportFragmentManager());
        bottomDialog.setViewListener(new BottomDialog.ViewListener() {
            @Override
            public void bindView(View v) {
                v.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        bottomDialog.dismiss();
                    }
                });
                View camera = v.findViewById(R.id.camera);
                camera.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        temp_cmera = new File(getExternalCacheDir().getAbsolutePath() + "/" + UUID.randomUUID().toString() + ".jpg");
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.putExtra("return-data", true);
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                            Uri contentUri = FileProvider.getUriForFile(UserShimingActivity.this, BuildConfig.APPLICATION_ID + ".fileProvider", temp_cmera);
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
                        } else {
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(temp_cmera));
                        }
                        startActivityForResult(intent, REQUEST_CAMERA);
                        bottomDialog.dismiss();
                    }
                });
                View exit = v.findViewById(R.id.exit);
                exit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        bottomDialog.dismiss();
                    }
                });
                View imgcontent = v.findViewById(R.id.imgcontent);
                imgcontent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
                        intent.setType("image/*");
                        intent.putExtra("return-data", true);
                        startActivityForResult(intent, REQUEST_IMAGECOUNT);
                        bottomDialog.dismiss();
                    }
                });
            }
        });
        bottomDialog.setLayoutRes(R.layout.dialog_uploadimg);
        bottomDialog.setDimAmount(0.5f);
        bottomDialog.setCancelOutside(true);
        bottomDialog.setTag("BottomDialog");

        bottomDialog1 = BottomDialog.create(getSupportFragmentManager());
        bottomDialog1.setViewListener(new BottomDialog.ViewListener() {
            @Override
            public void bindView(View v) {
                v.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        bottomDialog1.dismiss();
                    }
                });
                View camera = v.findViewById(R.id.camera);
                camera.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        temp_cmera = new File(getExternalCacheDir().getAbsolutePath() + "/" + UUID.randomUUID().toString() + ".jpg");
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.putExtra("return-data", true);
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                            Uri contentUri = FileProvider.getUriForFile(UserShimingActivity.this, BuildConfig.APPLICATION_ID + ".fileProvider", temp_cmera);
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
                        } else {
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(temp_cmera));
                        }
                        startActivityForResult(intent, REQUEST_CAMERA1);
                        bottomDialog1.dismiss();
                    }
                });
                View exit = v.findViewById(R.id.exit);
                exit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        bottomDialog1.dismiss();
                    }
                });
                View imgcontent = v.findViewById(R.id.imgcontent);
                imgcontent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
                        intent.setType("image/*");
                        intent.putExtra("return-data", true);
                        startActivityForResult(intent, REQUEST_IMAGECOUNT1);
                        bottomDialog1.dismiss();
                    }
                });
            }
        });
        bottomDialog1.setLayoutRes(R.layout.dialog_uploadimg);
        bottomDialog1.setDimAmount(0.5f);
        bottomDialog1.setCancelOutside(true);
        bottomDialog1.setTag("BottomDialog1");






        bottomDialog2 = BottomDialog.create(getSupportFragmentManager());
        bottomDialog2.setViewListener(new BottomDialog.ViewListener() {
            @Override
            public void bindView(View v) {
                v.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        bottomDialog2.dismiss();
                    }
                });
                View camera = v.findViewById(R.id.camera);
                camera.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        temp_cmera = new File(getExternalCacheDir().getAbsolutePath() + "/" + UUID.randomUUID().toString() + ".jpg");
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.putExtra("return-data", true);
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                            Uri contentUri = FileProvider.getUriForFile(UserShimingActivity.this, BuildConfig.APPLICATION_ID + ".fileProvider", temp_cmera);
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
                        } else {
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(temp_cmera));
                        }
                        startActivityForResult(intent, REQUEST_CAMERA2);
                        bottomDialog2.dismiss();
                    }
                });
                View exit = v.findViewById(R.id.exit);
                exit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        bottomDialog2.dismiss();
                    }
                });
                View imgcontent = v.findViewById(R.id.imgcontent);
                imgcontent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
                        intent.setType("image/*");
                        intent.putExtra("return-data", true);
                        startActivityForResult(intent, REQUEST_IMAGECOUNT2);
                        bottomDialog2.dismiss();
                    }
                });
            }
        });
        bottomDialog2.setLayoutRes(R.layout.dialog_uploadimg);
        bottomDialog2.setDimAmount(0.5f);
        bottomDialog2.setCancelOutside(true);
        bottomDialog2.setTag("BottomDialog2");

    }

    @OnClick(value = {R.id.backbt, R.id.zhengmian_bt, R.id.fanmian_bt, R.id.submitbt, R.id.shouchi_bt})
    public void onMyClick(View view) {
        switch (view.getId()) {
            case R.id.backbt:
                finish();
                break;
            case R.id.zhengmian_bt:
                bottomDialog.show();
                break;
            case R.id.fanmian_bt:
                bottomDialog1.show();
                break;
            case R.id.shouchi_bt:
                bottomDialog2.show();
                break;
            case R.id.submitbt:
                if (TextUtils.isEmpty(realnameEdit.getText().toString().trim())) {
                    Toast.makeText(getApplicationContext(), "真实姓名不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(cardEdit.getText().toString().trim())) {
                    Toast.makeText(getApplicationContext(), "身份证号不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (cardEdit.getText().toString().trim().length() < 15) {
                    Toast.makeText(getApplicationContext(), "确定是否是身份证号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(temppath1)) {
                    Toast.makeText(getApplicationContext(), "请上传正面身份证照片", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(temppath2)) {
                    Toast.makeText(getApplicationContext(), "请上传反面身份证照片", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(temppath3)) {
                    Toast.makeText(getApplicationContext(), "请上传本人手持身份证照片", Toast.LENGTH_SHORT).show();
                    return;
                }
                submit(realnameEdit.getText().toString().trim(),
                        cardEdit.getText().toString().trim(), temppath1, temppath2,temppath3);
                break;
        }
    }

    public void submit(String userRealname, String uerIdcard, String temppath1, String temppath2,String temppath3) {

        final List<User> userList = Xutils.queryAll(getApplication(), User.class);
        if (userList == null || userList.size() == 0) {
            return;
        }
        loadingdialog.show();
        RequestParams params = new RequestParams(HttpUrl.realnameupload);
        params.setCharset("UTF-8");
        params.setMultipart(true);
        params.addParameter("userId", userList.get(0).getUserId() + "");
        params.addParameter("userRealname", userRealname);
        params.addParameter("userIdcard", uerIdcard);
        params.addParameter("files", new File(temppath1));
        params.addParameter("files", new File(temppath2));
        params.addParameter("files", new File(temppath3));
        Callback.Cancelable cancelable = x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                loadingdialog.dismiss();
                try {
                    JSONObject json = new JSONObject(result);
                    Toast.makeText(getApplicationContext(), json.getString("message"), Toast.LENGTH_SHORT).show();
                    switch (json.getInt("state")) {
                        case HttpCode.SUCCESS:
                            finish();
                            break;
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                loadingdialog.dismiss();
                System.out.println("****************" + ex.getMessage());
                Toast.makeText(getApplicationContext(), "请稍后重试", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_IMAGECOUNT:
                Uri datauri = data.getData();
                final File finalFile1 = new File(getExternalCacheDir().getAbsolutePath() + "/" + UUID.randomUUID().toString().substring(0, 10) + ".jpg");
                ;
                temppath1 = finalFile1.getPath();
                Glide.with(this)
                        .load(datauri)
                        .asBitmap()
                        .override(500, 500)
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                zhengmianIv.setImageBitmap(resource);
                                Bitmap bitmap = null;
                                if (resource.getHeight() < 500) {
                                    bitmap = Tools.zoomBitmap(resource, resource.getWidth() / 1, resource.getHeight() / 1);
                                } else if (resource.getHeight() < 1000) {
                                    bitmap = Tools.zoomBitmap(resource, resource.getWidth() / 2, resource.getHeight() / 2);
                                } else {
                                    bitmap = Tools.zoomBitmap(resource, resource.getWidth() / 3, resource.getHeight() / 3);
                                }
                                try {
                                    bitmap.compress(Bitmap.CompressFormat.JPEG, 80, new FileOutputStream(finalFile1));
                                } catch (FileNotFoundException e) {
                                    e.printStackTrace();
                                }
                            }
                        });

                break;
            case REQUEST_CAMERA:
                Intent bgcamera = new Intent("com.android.camera.action.CROP");
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    bgcamera.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    Uri contentUri = FileProvider.getUriForFile(UserShimingActivity.this, BuildConfig.APPLICATION_ID + ".fileProvider", temp_cmera);
                    bgcamera.setDataAndType(contentUri, "image/*");
                } else {
                    bgcamera.setDataAndType(Uri.fromFile(temp_cmera), "image/*");
                }
                bgcamera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(temp_cmera));
                bgcamera.putExtra("return-data", false);
                bgcamera.putExtra("scale", true);
                bgcamera.putExtra("outputFormat", Bitmap.CompressFormat.PNG);
                bgcamera.putExtra("noFaceDetection", true);
                //   bgcamera.putExtra("aspectX",son1);
                //   bgcamera.putExtra("aspectY",son1);
                bgcamera.putExtra("outputX", 500);
                bgcamera.putExtra("outputY", 500);
                startActivityForResult(bgcamera, REQUEST_CROP);
                break;

            case REQUEST_CROP:
                temppath1 = temp_cmera.getPath();
                Glide.with(this)
                        .load(temppath1)
                        .asBitmap()
                        .override(500, 500)
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                Bitmap bitmap = null;
                                if (resource.getHeight() < 500) {
                                    bitmap = Tools.zoomBitmap(resource, resource.getWidth() / 1, resource.getHeight() / 1);
                                } else if (resource.getHeight() < 1000) {
                                    bitmap = Tools.zoomBitmap(resource, resource.getWidth() / 2, resource.getHeight() / 2);
                                } else {
                                    bitmap = Tools.zoomBitmap(resource, resource.getWidth() / 3, resource.getHeight() / 3);
                                }
                                try {
                                    bitmap.compress(Bitmap.CompressFormat.JPEG, 80, new FileOutputStream(temppath1));
                                } catch (FileNotFoundException e) {
                                    e.printStackTrace();
                                }
                                zhengmianIv.setImageBitmap(bitmap);
                            }
                        });


                break;
            case REQUEST_IMAGECOUNT1:
                Uri datauri1 = data.getData();
                final File finalFile2 = new File(getExternalCacheDir().getAbsolutePath() + "/" + UUID.randomUUID().toString() + ".jpg");
                temppath2 = finalFile2.getPath();
                Glide.with(this)
                        .load(datauri1)
                        .asBitmap()
                        .override(500, 500)
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                fanmianIv.setImageBitmap(resource);
                                Bitmap bitmap = null;
                                if (resource.getHeight() < 500) {
                                    bitmap = Tools.zoomBitmap(resource, resource.getWidth() / 1, resource.getHeight() / 1);
                                } else if (resource.getHeight() < 1000) {
                                    bitmap = Tools.zoomBitmap(resource, resource.getWidth() / 2, resource.getHeight() / 2);
                                } else {
                                    bitmap = Tools.zoomBitmap(resource, resource.getWidth() / 3, resource.getHeight() / 3);
                                }
                                try {
                                    bitmap.compress(Bitmap.CompressFormat.JPEG, 80, new FileOutputStream(finalFile2));
                                } catch (FileNotFoundException e) {
                                    e.printStackTrace();
                                }
                            }
                        });

                break;
            case REQUEST_CAMERA1:
                Intent bgcamera1 = new Intent("com.android.camera.action.CROP");
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    bgcamera1.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    Uri contentUri = FileProvider.getUriForFile(UserShimingActivity.this, BuildConfig.APPLICATION_ID + ".fileProvider", temp_cmera);
                    bgcamera1.setDataAndType(contentUri, "image/*");
                } else {
                    bgcamera1.setDataAndType(Uri.fromFile(temp_cmera), "image/*");
                }
                bgcamera1.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(temp_cmera));
                bgcamera1.putExtra("return-data", false);
                bgcamera1.putExtra("scale", true);
                bgcamera1.putExtra("outputFormat", Bitmap.CompressFormat.PNG);
                bgcamera1.putExtra("noFaceDetection", true);
                //   bgcamera.putExtra("aspectX",son1);
                //     bgcamera.putExtra("aspectY",son1);
                bgcamera1.putExtra("outputX", 500);
                bgcamera1.putExtra("outputY", 500);
                startActivityForResult(bgcamera1, REQUEST_CROP1);
                break;
            case REQUEST_CROP1:
                temppath2 = temp_cmera.getPath();
                Glide.with(this)
                        .load(temppath2)
                        .asBitmap()
                        .override(500, 500)
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                Bitmap bitmap = null;
                                if (resource.getHeight() < 500) {
                                    bitmap = Tools.zoomBitmap(resource, resource.getWidth() / 1, resource.getHeight() / 1);
                                } else if (resource.getHeight() < 1000) {
                                    bitmap = Tools.zoomBitmap(resource, resource.getWidth() / 2, resource.getHeight() / 2);
                                } else {
                                    bitmap = Tools.zoomBitmap(resource, resource.getWidth() / 3, resource.getHeight() / 3);
                                }
                                try {
                                    bitmap.compress(Bitmap.CompressFormat.JPEG, 80, new FileOutputStream(temppath2));
                                } catch (FileNotFoundException e) {
                                    e.printStackTrace();
                                }
                                fanmianIv.setImageBitmap(resource);
                            }
                        });

                break;




            case REQUEST_IMAGECOUNT2:
                Uri datauri2 = data.getData();
                final File finalFile3 = new File(getExternalCacheDir().getAbsolutePath() + "/" + UUID.randomUUID().toString() + ".jpg");
                temppath3 = finalFile3.getPath();
                Glide.with(this)
                        .load(datauri2)
                        .asBitmap()
                        .override(500, 500)
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                shouchiIv.setImageBitmap(resource);
                                Bitmap bitmap = null;
                                if (resource.getHeight() < 500) {
                                    bitmap = Tools.zoomBitmap(resource, resource.getWidth() / 1, resource.getHeight() / 1);
                                } else if (resource.getHeight() < 1000) {
                                    bitmap = Tools.zoomBitmap(resource, resource.getWidth() / 2, resource.getHeight() / 2);
                                } else {
                                    bitmap = Tools.zoomBitmap(resource, resource.getWidth() / 3, resource.getHeight() / 3);
                                }
                                try {
                                    bitmap.compress(Bitmap.CompressFormat.JPEG, 80, new FileOutputStream(finalFile3));
                                } catch (FileNotFoundException e) {
                                    e.printStackTrace();
                                }
                            }
                        });

                break;
            case REQUEST_CAMERA2:
                Intent bgcamera2 = new Intent("com.android.camera.action.CROP");
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    bgcamera2.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    Uri contentUri = FileProvider.getUriForFile(UserShimingActivity.this, BuildConfig.APPLICATION_ID + ".fileProvider", temp_cmera);
                    bgcamera2.setDataAndType(contentUri, "image/*");
                } else {
                    bgcamera2.setDataAndType(Uri.fromFile(temp_cmera), "image/*");
                }
                bgcamera2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(temp_cmera));
                bgcamera2.putExtra("return-data", false);
                bgcamera2.putExtra("scale", true);
                bgcamera2.putExtra("outputFormat", Bitmap.CompressFormat.PNG);
                bgcamera2.putExtra("noFaceDetection", true);
                //   bgcamera.putExtra("aspectX",son1);
                //     bgcamera.putExtra("aspectY",son1);
                bgcamera2.putExtra("outputX", 500);
                bgcamera2.putExtra("outputY", 500);
                startActivityForResult(bgcamera2, REQUEST_CROP2);
                break;
            case REQUEST_CROP2:
                temppath3 = temp_cmera.getPath();
                Glide.with(this)
                        .load(temppath3)
                        .asBitmap()
                        .override(500, 500)
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                Bitmap bitmap = null;
                                if (resource.getHeight() < 500) {
                                    bitmap = Tools.zoomBitmap(resource, resource.getWidth() / 1, resource.getHeight() / 1);
                                } else if (resource.getHeight() < 1000) {
                                    bitmap = Tools.zoomBitmap(resource, resource.getWidth() / 2, resource.getHeight() / 2);
                                } else {
                                    bitmap = Tools.zoomBitmap(resource, resource.getWidth() / 3, resource.getHeight() / 3);
                                }
                                try {
                                    bitmap.compress(Bitmap.CompressFormat.JPEG, 80, new FileOutputStream(temppath3));
                                } catch (FileNotFoundException e) {
                                    e.printStackTrace();
                                }
                                shouchiIv.setImageBitmap(resource);
                            }
                        });

                break;


        }
    }

    public void webUserServer() {
        final List<User> userList = Xutils.queryAll(getApplication(), User.class);
        if (userList == null || userList.size() == 0) {
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("userId", userList.get(0).getUserId() + "");
        Xutils.POST(HttpUrl.queryuserbyid, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                System.out.println("*******************" + result);
                Gson gson = new Gson();
                UserDomain userDomain = gson.fromJson(result, UserDomain.class);
                switch (userDomain.getState()) {
                    case HttpCode.SUCCESS:
                        User user = userDomain.getData();
                        user.setId(userList.get(0).getId());
                        Xutils.update(getApplication(), user);
                        break;
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (temp_cmera != null) {
            temp_cmera.delete();
        }
        if (!TextUtils.isEmpty(temppath1)) {
            File file = new File(temppath1);
            System.out.println("*************temppath1:" + temppath1);
            file.delete();

        }
        if (!TextUtils.isEmpty(temppath2)) {
            File file = new File(temppath2);
            System.out.println("*************temppath2:" + temppath2);
            file.delete();

        }
    }
}
